package cn.cxyxj.study110.component;

import cn.cxyxj.study110.entity.SysMenu;
import cn.cxyxj.study110.mapper.SysMenuMapper;
import cn.cxyxj.study110.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 初始化启动组件
 * @author: cxyxj
 */
@Component
@Slf4j
public class InitializationRunner implements ApplicationRunner {

    @Autowired
    private SysMenuMapper mapper;


    @Override
    public void run(ApplicationArguments args) {
        log.info("================ 程序初始化方法开始执行 ================");
        List<SysMenu> sysMenus = mapper.selectList(null);
        if(!CollectionUtils.isEmpty(sysMenus)){
            Set<String> urlAll = sysMenus.stream().map(SysMenu::getApiUrl).collect(Collectors.toSet());
            for (String url : urlAll) {
                RedisUtil.sSet("all-url",url);
            }
        }
        log.info("================ 程序初始化方法执行结束 ================");
    }
}